package gameEngine.agents.observationSystem.analyzer;

import java.util.HashMap;

import gameEngine.agents.observationSystem.samples.Sample;
import gameEngine.agents.teamMateModel.AgentMock;
import gameEngine.agents.teamMateModel.TeamMateModel;

/**
 *  @author vladimir && alon
 *	
 *	The class is analyze the observation by calculating the average
 */
public class AverageAnalyzer implements ObservationAnalyzer{
	
	private TeamMateModel teamateModel;
	private EpsilonAnalyzer epsilonAnalyzer;
	/** 
	 * number of rounds that was played for every agent
	 * rounds[0] for agent 1, rounds[1] for agent 2 and so on
	 * */
	//private int rounds[];
	private HashMap<Integer,Integer> rounds;
	/**
	 * CTOR
	 * @param teamateModel
	 * @param numOfAgents
	 */
	public AverageAnalyzer(TeamMateModel teamateModel)
	{
		
		rounds=new HashMap<Integer,Integer>();
		for (AgentMock a:teamateModel.getTeamMates())
		{
			rounds.put(a.getId(), 0);
		}
		this.teamateModel=teamateModel;
		epsilonAnalyzer=new EpsilonAnalyzer(teamateModel);
	}
	
	/**
	 * @param observation (type of Sample)
	 */
	public void analyze(Sample observation) {
		// TODO Auto-generated method stub
		if(observation == null){ return; }
		
		//take the agentMok (representation of agent) from the teamateModel 
		int id = observation.getID();
		//nothing to do as we dont need to explore our self
		if(teamateModel.getSelf().getId()==id){return;}
		
		AgentMock agentMock = teamateModel.getAgentById(id);
		
		int round = rounds.get(id);
		//case the first observation for agent with this id
		//if(round == 0)
		//{
		//	agentMock.setAgentCompitance(observation.getScore());
		//}
		//case before was observations for this player
		//else
		//{
			// new compitance
			double compitance = agentMock.getAgentCompitance();
			compitance = (compitance*round + observation.getScore()) / (round + 1);
			agentMock.setAgentCompitance(compitance);
		//}
		
		//update the number of the rounds, "- 1" because rounds start from zero 
		rounds.put(id, round);
		epsilonAnalyzer.analyze(observation);
	}
	
	public AverageAnalyzer clone(){
		return new AverageAnalyzer(teamateModel.clone());
	}
	
	public AverageAnalyzer initAnalyzer(TeamMateModel model){
		return new AverageAnalyzer(model);
	}

}
